"use strict";

angular.module('app.yksensor').controller('YKAlertsiteCtrl', function ($scope, $http,$cookieStore) {

    //配置分页基本参数
    $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 10,
        pagesLength: 5,
        totalItems: 20,
        perPageOptions: [10, 20, 30, 40, 50],

        onChange: function () {
        }
    };
    $scope.isTemp = false;
    $scope.changeField = function(value){
        // console.log(value)
        if(value == "temp"){
            $scope.isTemp = true;
        }else{
            $scope.isTemp = false;
        }
    }
    

    //设备编号选择
    $http.get(baseurls+"/list_nopage/device").then(function(res){
        if(res.data.ret == 1){
            // console.log(res.data.info);
            $scope.alldevice = res.data.info;
        }
    })
    //1.报警列表
    $scope.select = function(){
        // $http.get(baseurls+"/list_nopage/warn_config").then(function(res){
        //     if(res.data.ret == 1){
        //         // console.log(res.data.info);
        //         $scope.datas = res.data.info;
        //         $scope.paginationConf.totalItems = res.data.info.length;
        //     }
        // })
        $http.post(baseurls+"/list/select/warn_config", $scope.paginationConf).then(function(res){
            // console.log(res.data)
            if(res.data.ret == 1){
                $scope.datas = res.data.rows;
                $scope.paginationConf.totalItems = res.data.records;
            }
            
        })
    }
    $scope.select();
    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
    
    //2.添加报警设置
    $scope.insert = {};
    $scope.insertdata = function () {
        $scope.temp = {};
        $scope.insert.fid = $cookieStore.get("user");
        $scope.insert.time = getNowFormatDate();
        $scope.insert.notice_type = 4;
        $scope.insert.postaddress = "http://120.27.18.161:3001/api/system/warn";
        $scope.insert.warn_grade = "1";
        // console.log($scope.insert);
        $http.post(baseurls+"/data/warn_config",$scope.insert).then(function(res){
            // console.log(res.data);
            if(res.data.ret == 1){
                // console.log($scope.insert);
                new Notify('success','添加报警事件成功');
            }else if(res.data.ret != -4){
                new Notify('fail','添加报警事件失败');
            }
            $("#addAlert").modal("hide");
            $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);//更新列表
        })
    }

    //3.编辑报警事件
    $scope.editdata = {};
    $scope.showEditAlert = function (data) {
        // console.log(data)
        $scope.editdata.id = data.id;
        $scope.editdata.deviceid = data.deviceid;
        $scope.editdata.warn_type = data.warn_type.toString();
        // $scope.editdata.notice_type = data.notice_type.toString();
        // $scope.editdata.warn_grade = data.warn_grade;
        // $scope.editdata.notice_type = 4;
        $scope.editdata.fieldname = data.fieldname;
        $scope.editdata.value = data.value;
        $scope.isTemp = false;
        if($scope.editdata.value.indexOf(",") >= 0){
            $scope.isTemp = true;
        }
        // $scope.editdata.time = data.time;
        

        $('#editAlert').modal('show');
    }
    $scope.updatedata = function () {
        $scope.editdata.fid = $cookieStore.get("user");
        // console.log($scope.editdata)
        $http.put(baseurls+"/data/warn_config?id=="+$scope.editdata.id,$scope.editdata).then(function(res){
            // console.log($scope.editdata.id,$scope.editdata)
            // console.log(res.data)
            if(res.data.ret == 1){
                new Notify('success','报警设置修改成功');
            }else if(res.data.ret != -4){
                new Notify('fail','报警设置修改失败');
            }
            $("#editAlert").modal("hide");
            $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
        })

    }
    //4. 删除报警事件
    $scope.delid = {};
    $scope.showDelDialog = function (id) {
        $scope.delid = id;
        $('#showDelDialog').modal('show');
    }
    $scope.deldevice = function () {
        $http.delete(baseurls+"/data/warn_config?id=="+$scope.delid).then(function(res){
            if(res.data.ret == 1){
                new Notify('success','删除报警事件成功');
            }else if(res.data.ret != -4){
                new Notify('fail','删除报警事件失败');
            }
            $('#showDelDialog').modal('hide');
            $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', $scope.select);
        })  
    }

});
